package it.unibo.imola.parser.visitor.entity;

import it.unibo.imola.utils.ToStringUtils;

import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BuiltinCallStatement extends CallStatement {

	private Logger logger = LoggerFactory.getLogger(getClass());

	public BuiltinCallStatement(String name, ToolBox toolbox) {
		super(name, toolbox);
	}

	@Override
	public boolean check(GlobalEnvironment gEnv, Environment env) throws Exception {
		if (super.check(gEnv, env)) {

			// qui vado a vedere se c'è un metodo nella toolbox con quel nome e
			// con
			// quei parametri
			// sarebbe meglio fare il controllo qui ma ormai avevo già
			// un'implementazione fatta...
			if (!toolbox.exists(name, actualParameterType)) {
				if (logger.isDebugEnabled())
					logger.debug(getClass().getSimpleName() + " does not contains CALL \"" + name + "\" " + ArrayUtils.toString(actualParameterType) + " ");

				throw new Exception(name + ": check: the signature " + ToStringUtils.toString(actualParameterType) +" is not compatible with BUILTIN CALL \"" + name + "\"");
			}
			return true;
		}
		return false;
	}
	
	@Override
	public void exec(GlobalEnvironment gEnv, Environment env) throws Exception {
		// TODO Auto-generated method stub
		super.exec(gEnv, env);
		try {
			toolbox.exec(name, unboxedActualParameters);
		} catch (Exception e) {
			throw new Exception(name + ": exec: " + e.getMessage());
		}
	}

}
